﻿package Classes.Player{
	import flash.display.*;
	import flash.events.*;
	import flash.utils.*;
		
	import Classes.*;
	import Classes.Player.*;
	
	public class Shield extends MovieClip{		
		//是否可以使用防护罩
		public var shieldable:Boolean;
		//防护罩所需能量
		private var energy:Number = 60;
		//
		private var _canshield:Boolean;
		//防护罩状态。0：没有使用，1开启阶段，2关闭阶段
		private var shieldstate:int;
		//所有者
		public var master:Fighter;
		//防护时间
		private var shieldtime:int = 10000;
		private var timer:Timer;
		//构造函数
		public function Shield(){	
			shieldable = true;
			shieldstate = 0;
			timer = new Timer(shieldtime);
			addEventListener(Event.ENTER_FRAME,shieldFrameHandler);
		}
		
		private function shieldFrameHandler(e:Event):void{			
			shield();
			if(this.currentFrame<this.totalFrames)
				this.nextFrame()
			else
				this.gotoAndPlay(1);
		}
		//防护中
		private function shield():void{
			//开启阶段
			if(shieldstate == 1){
				if(this.alpha<1)
					this.alpha += .1;
				else
					shieldstate = 0;					
			}
			//关闭阶段
			else if(shieldstate == 2){
				if(this.alpha>0)
					this.alpha -= .05;
				else{
					shieldstate = 0;
					master.hurtable = true;
					if(master.getChildAt(master.numChildren -1) is Shield){
						master.removeChild(this);
						master.hurtable = true;
					}					
				}
			}				  			
		}
		//清除防护罩
		private function clearShield(e:TimerEvent):void{
			shieldable = true;
			canShield = false;
			shieldstate = 2;
			timer.removeEventListener(TimerEvent.TIMER,clearShield);
			timer.reset();
		}
		
		public function set canShield(value:Boolean):void{		
			_canshield = value;			
			if(master.energy < energy)
				_canshield = false;
			if(_canshield){			
				master.addChild(this);				
				shieldstate = 1;
				master.energy -= energy;
				master.hurtable = false;
				shieldable = false;
				timer.addEventListener(TimerEvent.TIMER,clearShield);			
				timer.start();
			}
			else{
				shieldstate = 2;
			}			
		}
		
		public function get canShield():Boolean{
			return _canshield;
		}
	}
}		